<?php

namespace app\mini\controller;

use app\common\controller\Adminbase;
use app\Constant\JumpTypeConstant;
use app\model\GoodsColumn;
use app\model\Swiper as SwiperModel;

class Swiper extends Adminbase
{
    public function index()
    {
        $model = 'Swiper';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list   = SwiperModel::where($map)
                ->field('*')
                ->withAttr('image', function ($value) {
                    return $value ? get_file_path($value, 1) : '';
                })
                ->withAttr('type', function ($value, $data) {
                    if ($value == 0 || $value == 4) {
                        return JumpTypeConstant::TYPES[$value];
                    } else {
                        if ($value == 1) {
                            return '系统功能：' . JumpTypeConstant::SYS[$data['value']];
                        } elseif ($value == 2) {
                            return '活动：' . JumpTypeConstant::ACTIVITYS[$data['value']];
                        } else {
                            return '活动专辑：' . GoodsColumn::where('id', $data['value'])->value('title');
                        }
                    }
                })
                ->page($page, $pageSize)
                ->order('sort', 'asc')
                ->order('id', 'asc')
                ->select();
            $total  = SwiperModel::where($map)->count();
            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setDataUrl()
            ->setFormUrl('form')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'status')
            ->setSwitchTpl('amplifyTpl', 'image')
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('image', '图片', null, '#amplifyTpl')
            ->addTableColumn('type', '跳转')
            ->addTableColumn('sort', '排序', null, null, false, '', false, '', true)
            ->addTableColumn('status', '是否显示', null, '#switchTpl', false, '', false)
            ->addTableOperation()
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'Swiper';

        $id = $this->request->param('id/d');
        if (isset($id) && $id > 0) {
            $info = SwiperModel::get($id);
            switch ($info['type']) {
                case 1:
                    $info['sys'] = $info['value'];;
                    break;
                case 2:
                    $info['activity'] = $info['value'];;
                    break;
                case 3:
                    $info['column'] = $info['value'];;
                    break;
            }

            $this->assign('id', $id);
            $this->assign('action', 2);
        } else {
            $info = [
                'sort' => 50,
                'type' => 0,
            ];
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $type = $data['type'];

            if ($type != 0) {
                if ($type == 4) {
                    $data['value'] = '';
                    if (empty($data['appid']) || empty($data['app_path'])) {
                        return json(['code' => -1, 'msg' => '请填写外部小程序appid和路径']);
                    }
                } else {
                    if ($type == 1) {
                        $data['value'] = $data['sys'];
                    } elseif ($type == 2) {
                        $data['value'] = $data['activity'];
                    } else {
                        $data['value'] = $data['column'];
                    }
                    if (empty($data['value'])) {
                        return json(['code' => -1, 'msg' => '请选择跳转值']);
                    }
                    $data['appid'] = $data['app_path'] = '';
                }
            } else {
                $data['value'] = 0;
                $data['appid'] = $data['app_path'] = '';
            }

            if ($data['action'] == 1) {
                unset($data['id']);
                $res = SwiperModel::create($data);
            } else {
                $res = saveDataByModel(SwiperModel::class, $data, ['id' => $data['id']]);
            }
            if ($res) {
                $result = ['code' => 0, 'msg' => '操作成功'];
            } else {
                $result = ['code' => -1, 'msg' => '操作失败'];
            }
            return json($result);
        }

        $date    = date('Y-m-d');
        $columns = GoodsColumn::where('end_date', 'egt', $date)->column('id, title');

        $builder = new \app\common\controller\FormBuilder();
        $builder
            ->setUrl('form')
            ->setModel($model)
            ->setSuccessUrl()
            ->addFormItem('image', 'image', '图片', '尺寸：1180 x 1032', [], 1)
            ->addFormItem('sort', 'gtnumber', '排序', '值越小排序越靠前')
            ->addFormItem('type', 'radio', '跳转类型', '', JumpTypeConstant::TYPES)
            ->addFormItem('sys', 'select', '跳转系统功能页', '', JumpTypeConstant::SYS)
            ->addFormItem('activity', 'select', '跳转活动页', '', JumpTypeConstant::ACTIVITYS)
            ->addFormItem('column', 'select', '活动专辑', '', $columns)
            ->addFormItem('appid', 'text', '外部小程序appid')
            ->addFormItem('app_path', 'text', '外部小程序路径', "<span style='color: red'>查询外部小程序指定页面路径方法:可以打开微信公众平台登录公众号账户进入,在内容与互动-草稿箱-新的创作-写新图文-小程序-去搜索指定小程序的更多页面路径</span>")
            ->setFormData($info)
            ->setExtraHtml($this->extra_html)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }

    private $extra_html = <<<EOF
    <script type="text/javascript">
        $(function(){
            layui.use(['form'], function () {
                var form = layui.form;
                //获取单选框name=qualification_type选择的值
                $('input[name="type"]').each(function(){
                    if ($(this).is(':checked')) {
                        handleTypeOption($(this).val())
                    }
                })
                form.on('radio(type)', function(data){
                    handleTypeOption(data.value)
                });
                function handleTypeOption(type) {
                    switch (parseInt(type)) {
                        case 1:
                            $('.item-sys').removeClass('layui-hide');
                            $('.item-activity').addClass('layui-hide');
                            $('.item-column').addClass('layui-hide');
                            $('.item-appid').addClass('layui-hide');
                            $('.item-app_path').addClass('layui-hide');
                            break;
                        case 2:
                            $('.item-activity').removeClass('layui-hide');
                            $('.item-sys').addClass('layui-hide');
                            $('.item-column').addClass('layui-hide');
                            $('.item-appid').addClass('layui-hide');
                            $('.item-app_path').addClass('layui-hide');
                            break;
                        case 3:
                            $('.item-column').removeClass('layui-hide');
                            $('.item-activity').addClass('layui-hide');
                            $('.item-sys').addClass('layui-hide');
                            $('.item-appid').addClass('layui-hide');
                            $('.item-app_path').addClass('layui-hide');
                            break;    
                        case 4:
                            $('.item-appid').removeClass('layui-hide');
                            $('.item-app_path').removeClass('layui-hide');
                            $('.item-column').addClass('layui-hide');
                            $('.item-sys').addClass('layui-hide');
                            $('.item-activity').addClass('layui-hide');
                            break;    
                        default:
                            $('.item-appid').addClass('layui-hide');
                            $('.item-app_path').addClass('layui-hide');
                            $('.item-column').addClass('layui-hide');
                            $('.item-sys').addClass('layui-hide');
                            $('.item-activity').addClass('layui-hide');    
                    }
                    
                } 
            });
        });
    </script>
EOF;
}
